<?php

/*
 * litchicms
 * Copyright (C) 2010-7-9 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

define('IS_BUILD_TPL', TRUE); //IS_BUILD_TPL 则解析
//Routing
$RT = array('v' => array(), 'r' => array());
$rt = explode("/", $_SERVER['REQUEST_URI']);
$rt = array_filter($rt);
foreach ($rt as $key => $value) {
    $temp = explode("-", $value);
    if (count($temp) > 1) {
        $RT['v'][$temp[0]] = $temp[1];
        key_exists(2, $temp) && $RT['r'][$temp[0]] = $temp[2];
    }
}
//dump($_SERVER['REQUEST_URI']);

(empty($RT['v']['site']) || $RT['v']['site'] != 'admin') && $RT['v']['site'] = '';
empty($RT['v']['mode']) && $RT['v']['mode'] = '';

$RT['v']['site'] == 'admin' && ($_SERVER['SCRIPT_NAME'] != "/" . $BRK_CONF['admin_file_name']) && header301('/' . $BRK_CONF['admin_file_name']);

//Routing   END

define("TPL_DIR", $BRK_CONF[$RT['v']['site'] . 'tpldir'] . $BRK_CONF[$RT['v']['site'] . $RT['v']['mode'] . 'tpl'] . '/');
define("TPL_CACHE_DIR", SYSTEM . "cache/tpl_" . $BRK_CONF[$RT['v']['site'] . $RT['v']['mode'] . 'tpl'] . '/');


//    dump(base64_encode($_SESSION['BASE64'] . "m-index/s-indexfun/a-logout"));
//    dump($RT);
//    exit;
//连接数据库
$_POST['action'] = '';
key_exists('postaction', $_POST) && $_POST['action'] = $_POST['postaction'];

//引入函数库
require 'bg.fun.php';
require 'main.fun.php';
$Db = openSQL();
check_make_dir(SYSTEM . 'cache');
//$BRK_MSG    是否OK | 提示信息str OR arr | 是否自动跳转[是否,地址=0上一页]
$BRK_MSG = array(0, array(), 0);
$BRK_IMMSG = array(); //直接显示的提示信息,不会终止程序
//不需要权限的操作 .统一放在indexfun下
$noRight = array(
    'jsconf', //JS配置
);

if (!empty($RT['v']['a']) && in_array($RT['v']['a'], $noRight)) {
    //  base64("m-index/s-indexfun/a-jsconf")
} elseif (empty($_SESSION['brk_login'])) {
    $RT['v']['m'] = 'index';
    $RT['v']['s'] = 'indexfun';
    $RT['v']['a'] = 'login';
} elseif (!key_exists('a', $RT['v']) || $RT['v']['a'] != 'logout') {
//    dump($RT);exit;
    if (empty($RT['v']['m']) || empty($RT['r']['m'])) {
        brk_get_rt('m');
        brk_get_rt('s');
        brk_get_rt('a');
    }
    if (empty($RT['v']['s']) || empty($RT['r']['s'])) {
        brk_get_rt('s');
        brk_get_rt('a');
    }
    if (empty($RT['v']['a']) || empty($RT['r']['a'])) {
        brk_get_rt('a');
    }

    $NAVI = array();
    $NAVI['m'] = $NAVI['s'] = $NAVI['a'] = array();
    $temp = brk_right_get_child(2);
    foreach ($temp as $v) {
        $NAVI['m'][] = array(
            'node' => base64('m-' . brk_right_get_node($v) . '-' . $v),
            'inthis' => ($v == $RT['r']['m']),
            'note' => brk_right_get_note($v)
        );
    }
    $temp = brk_right_get_child($RT['r']['m']);
    foreach ($temp as $v) {
        $NAVI['a'][$v] = array();
        $temp2 = brk_right_get_child($v);
        $temppre = 'm-' . $RT['v']['m'] . '-' . $RT['r']['m'] . '/s-' . brk_right_get_node($v) . '-' . $v;
        foreach ($temp2 as $value) {
            $NAVI['a'][$v][] = array(
                'node' => base64($temppre . '/a-' . brk_right_get_node($value) . '-' . $value),
                'inthis' => ($RT['r']['s'] == $v && brk_right_get_node($value) == $RT['v']['a']),
                'note' => brk_right_get_note($value)
            );
        }
        $NAVI['s'][] = array(
            'node' => base64($temppre),
            'inthis' => ($v == $RT['r']['s']),
            'note' => brk_right_get_note($v),
            'r' => $v
        );
    }

    $RT_M = 'm-' . brk_right_get_node($RT['r']['m']) . '-' . $RT['r']['m'];
    $RT_S = '/s-' . brk_right_get_node($RT['r']['s']) . '-' . $RT['r']['s'];
    $RT_A = '/a-' . brk_right_get_node($RT['r']['a']) . '-' . $RT['r']['a'];
    $BREADCRUMD = array();
    if ($RT['r']['a'] != -2) {
        $BREADCRUMD[] = array(
            'node' => base64($RT_M . $RT_S . $RT_A),
            'note' => brk_right_get_note($RT['r']['a'])
        );

        $BREADCRUMD[] = array(
            'node' => base64($RT_M . $RT_S),
            'note' => brk_right_get_note($RT['r']['s'])
        );
        $BREADCRUMD[] = array(
            'node' => base64($RT_M),
            'note' => brk_right_get_note($RT['r']['m'])
        );
    }
    is_file($inc = SYSTEM . "litchi/" . $RT['v']['m'] . "/inc.php") && require $inc;
}

//引入模块处理文件

if (!empty($RT['r']['a']) && $RT['r']['a'] == -2) {
    $phpfile = SYSTEM . "litchi/noaction.php";
    $tplfile = 'public/noaction.php';
} else {
    $phpfile = SYSTEM . "litchi/" . $RT['v']['m'] . "/" . $RT['v']['s'] . "/" . $RT['v']['a'] . '.php';
    $tplfile = 'sys/' . $RT['v']['m'] . '/' . $RT['v']['s'] . '/' . $RT['v']['a'] . '.php';
}
if (is_file($phpfile)) {
    require $phpfile;
} else {
    dump($inc . '不存在');
    exit;
    header301("/index?admin");
}

//引入模板文件
require 'template.fun.php';
include template($tplfile);


$debug = array(
    "时间消耗:" . (microtime(TRUE) - $starttime) . "s",
    "内存消耗:" . (memory_get_usage() / 1024 / 1024) . "mb",
    $phpfile
);
